Skip to content

Add VALID_MODES constant and validate tracker mode at initialization#186

Open
JuanVqz wants to merge 5 commits into
mainfrom
feature/valid-modes-constant
Open

Add VALID_MODES constant and validate tracker mode at initialization#186
JuanVqz wants to merge 5 commits into
mainfrom
feature/valid-modes-constant

Conversation

@JuanVqz
Copy link
Copy Markdown
Member

@JuanVqz JuanVqz commented May 18, 2026

Description

Introduces DeprecationTracker::VALID_MODES as a single source of truth for accepted tracker modes (save, compare). Constants live in lib/deprecation_tracker/valid_modes.rb so the exe/deprecations CLI can load them without pulling in the full library.

Motivation and Context

Valid mode values were duplicated as inline literals across lib/deprecation_tracker.rb and exe/deprecations. Adding or removing a mode required updating every literal. With VALID_MODES, one change propagates to all validation, error messages, and CLI help text automatically.

Additionally, passing an invalid mode previously silently no-oped (neither save nor compare ran after the test suite). It now raises ArgumentError early, covering both the library and CLI entry points.

How Has This Been Tested?

  • Updated existing test that expected silent no-op on invalid mode to now expect ArgumentError
  • Added test for invalid ENV['DEPRECATION_TRACKER'] value via init_tracker
  • Added VALID_MODES describe block: constant membership and all valid modes construct without error
  • Full spec suite passes (bundle exec rspec spec/deprecation_tracker_spec.rb)

Screenshots:

N/A

I will abide by the code of conduct

Centralizes valid tracker modes so adding/removing an option requires
a single change. Raises ArgumentError early instead of silently no-oping
on invalid input, covering both library and CLI entry points.
@JuanVqz JuanVqz self-assigned this May 18, 2026
@JuanVqz JuanVqz marked this pull request as ready for review May 18, 2026 20:39
@JuanVqz JuanVqz requested review from arielj and etagwerker May 18, 2026 20:39
Extract constants to valid_modes.rb so exe/deprecations only loads
the full lib when needed. Use VALID_MODES_DISPLAY to avoid repeated
map/join. Loosen spec assertions to not couple on mode count or message wording.
Copy link
Copy Markdown
Member

@etagwerker etagwerker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuanVqz I think this might introduced an unwanted side effect (?)

bin/deprecations run without --tracker-mode is probably broken. The CLI builds:

command = "DEPRECATION_TRACKER=#{tracker_mode} #{rspec_command} ..."

When tracker_mode is nil, the spawned child sees ENV["DEPRECATION_TRACKER"] = ""

Empty string is truthy in Ruby, so:

mode = opts[:mode] || ENV["DEPRECATION_TRACKER"] || :save  # => ""
@mode = mode ? mode.to_sym : :save                          # => :""
# new check raises ArgumentError

Before this PR that silently no-op'd in after_run. After this PR it crashes at construction.

Comment thread spec/deprecation_tracker_spec.rb Outdated
Comment thread spec/deprecation_tracker_spec.rb Outdated
Comment thread lib/deprecation_tracker/valid_modes.rb Outdated
- Treat a blank mode (from opts[:mode] or DEPRECATION_TRACKER) as unset
  and default to :save instead of raising, so the documented
  `mode: ENV["DEPRECATION_TRACKER"]` wiring no longer crashes on an empty
  value. Add DeprecationTracker.sanitize_mode for this.
- Add DeprecationTracker.valid_mode? as the single source of truth for
  mode validation, used by both the constructor and the deprecations CLI.
- Replace VALID_MODES_DISPLAY constant with a memoized
  .valid_modes_display class method.
- bin/deprecations run now aborts with a clear message when --tracker-mode
  is missing or invalid.
- Specs: assert the ArgumentError message names the bad value, cover blank
  env/opts defaulting, and add .valid_mode?/.valid_modes_display blocks.
@JuanVqz JuanVqz requested review from arielj and etagwerker May 27, 2026 19:55
Comment thread spec/deprecation_tracker_spec.rb Outdated
Comment thread spec/deprecation_tracker_spec.rb
Comment thread lib/deprecation_tracker.rb Outdated
Address review on PR #186:
- Reformat invalid-mode ArgumentError to 'one of: save, compare. Got: ...'
  for clearer reading; update matching spec regexes.
- Remove tautological VALID_MODES 'contains save and compare' test.
@JuanVqz JuanVqz requested a review from arielj May 29, 2026 19:06
@JuanVqz
Copy link
Copy Markdown
Member Author

JuanVqz commented May 29, 2026

@arielj addressed latest code changes 👍

Comment thread exe/deprecations
Comment on lines 10 to +13
tracker_mode = opts[:tracker_mode]
unless DeprecationTracker.valid_mode?(tracker_mode)
abort "Invalid --tracker-mode #{tracker_mode.inspect}. Must be one of: #{DeprecationTracker.valid_modes_display}."
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this guard skip the case where no --tracker-mode is passed? --tracker-mode is optional, so on the documented default deprecations run (README + the help examples) opts[:tracker_mode] is nil, and valid_mode?(nil) is falsey, so this aborts with Invalid --tracker-mode nil. before reaching rspec.

I cloned the branch and confirmed it: deprecations run (no --tracker-mode) exits 1 on this branch, whereas on main it falls through to DEPRECATION_TRACKER= bundle exec rspec ... and the library treats the blank value as the default save. That blank-as-default path is exactly what sanitize_mode adds for the library, but the CLI guard rejects nil/blank before that logic runs.

What about only validating when a mode was actually supplied, so a genuine typo (--tracker-mode bogus) still fails loudly but the default keeps working?

Suggested change
tracker_mode = opts[:tracker_mode]
unless DeprecationTracker.valid_mode?(tracker_mode)
abort "Invalid --tracker-mode #{tracker_mode.inspect}. Must be one of: #{DeprecationTracker.valid_modes_display}."
end
tracker_mode = DeprecationTracker.sanitize_mode(opts[:tracker_mode])
if tracker_mode && !DeprecationTracker.valid_mode?(tracker_mode)
abort "Invalid --tracker-mode #{tracker_mode.inspect}. Must be one of: #{DeprecationTracker.valid_modes_display}."
end

Copy link
Copy Markdown
Member

@etagwerker etagwerker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one last comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants